/**
 * 
 */
package com.mvc.controller.healthmanager;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.model.base.Json;
import com.mvc.model.healthmanager.archive.Teeth;
import com.mvc.model.healthmanager.archive.Urine;
import com.mvc.service.healthmanager.UrineServiceI;

/**
 * @author qingjie
 * 尿液分析信息控制器
 */
@RequestMapping("/urine")
@Controller
public class UrineController {

	@Autowired
	private UrineServiceI usi;
	
	@RequestMapping("/saveUrine")
	@ResponseBody
	public Json saveUrine(HttpServletRequest request,Urine urine) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
//		json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		Urine u = new Urine();
		if (!json.isEmpty()) {
			u = JSON.parseObject(json, Urine.class);
			u.setId(null);
		}
		urine = usi.saveUrine(u);
		Json j = new Json();
		if(urine!=null&&!json.isEmpty()){
			j.setMsg("保存成功");
			j.setSuccess(true);
			j.setObj(urine);
		}else{
			j.setMsg("保存失败");
			j.setSuccess(false);
		}
		return j;
	}
	
//	@RequestMapping(value="/saveUrine", method=RequestMethod.POST,consumes = "application/json")
//	@ResponseBody
//	public Json saveUrine(@RequestBody Urine u,HttpServletRequest request,Urine urine) throws Exception{
//		u.setId(null);
//		urine = usi.saveUrine(u);
//		Json j = new Json();
//		if(urine!=null){
//			j.setMsg("保存成功");
//			j.setSuccess(true);
//			j.setObj(urine);
//		}else{
//			j.setMsg("保存失败");
//			j.setSuccess(false);
//		}
//		return j;
//	}
	
	@RequestMapping(value="/saveUrine", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json saveUrine(@RequestBody List<Urine> us,HttpServletRequest request) throws Exception{
		System.out.println("未转化的数据："+JSON.toJSONString(us));
		List<Urine> list = new ArrayList<Urine>();
		Json j = new Json();
		if(us.size()>0){
			for(Urine u : us){
				Long appLocalId = ValidatorUtil.isNullOnLong(u.getId())?null:u.getId();
				u.setId(null);
				Urine urine = new Urine(); 
				urine = usi.saveUrine(u);
				if(urine!=null&&!ValidatorUtil.isNullOnLong(urine.getId())){
					urine.setRespCode(0);
				}else{
					urine.setRespCode(-1);
				}
				urine.setAppLocalId(appLocalId);
				list.add(urine);
			}
			j.setMsg("数据已处理");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空");
			j.setSuccess(false);
		}
		return j;
	}
}
